package com.shuwen.gcdj.service.statistic;

import com.shuwen.gcdj.bean.conditionMonitoring.request.StrainSensorQueryReq;
import com.shuwen.gcdj.bean.conditionMonitoring.response.StatisticsTrendstabilityResp;
import com.shuwen.gcdj.bean.conditionMonitoring.response.StrainSensorTendencyResp;
import com.shuwen.gcdj.bean.sysDictionary.request.SysDictionaryReq;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.entity.statistics.StatisticsTrendstability;
import com.shuwen.gcdj.mapper.mysql.Statistic.StatisticsTrendstabilityMapper;
import com.shuwen.gcdj.mapper.mysql.sysDictionary.SysDictionaryMapper;
import com.shuwen.gcdj.service.BaseService;
import com.shuwen.gcdj.service.sysDictionary.SysDictionaryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 健康评估趋势平稳度
 *
 * @author Yangx
 */
@Service
@AllArgsConstructor
@Slf4j
public class StatisticsTrendstabilityService extends BaseService<StatisticsTrendstabilityMapper, StatisticsTrendstability> {
    private final StatisticsTrendstabilityMapper statisticsTrendstabilityMapper;
    private final SysDictionaryMapper sysDictionaryMapper;
    private final SysDictionaryService sysDictionaryService;

    public static double average(Double[] nums) {
        Double sum = 0.0;
        for (Double num : nums) {
            sum += num;
        }
        return sum / nums.length;
    }

    public CommonResult<StrainSensorTendencyResp> getStrainSensorTendency(StrainSensorQueryReq req) {
        StrainSensorTendencyResp resp = new StrainSensorTendencyResp();
        LocalDate date = LocalDate.now();
        List<String> dateValue = new ArrayList<>();
        List<Double> healthValue = new ArrayList<>();
        SysDictionaryReq dictionaryReq = new SysDictionaryReq();
        dictionaryReq.setName(req.getMonitorLocation());
        dictionaryReq.setParentCode(Constants.STATISTICS_TRENDSTABILITY_MONITOR_LOCATION);
        SysDictionaryResp sysDictionaryResp = sysDictionaryMapper.getSingle(dictionaryReq);
        if (sysDictionaryResp != null) {
            req.setMonitorLocationValue(sysDictionaryResp.getValue());
        }
        //柔腿趋势处理
        if (req.getMonitorLocationValue().equals(Constants.HOME_MAIN_STRUCTURE_VALUE))
        {
            List<SysDictionaryResp> sysDictionarylist = sysDictionaryService.getChildByParentCode(Constants.TILT_HISTORY_POINT_LOCATION);
            if(!CollectionUtils.isEmpty(sysDictionarylist))
            {
                SysDictionaryResp singleParam = sysDictionarylist.stream().filter(dict->dict.getCode().equals(req.getParentCode())).findFirst().orElse(null);
                if(singleParam!=null)
                {
                    req.setEvaluateObject(singleParam.getName());
                }
            }
        }
        switch (req.getStatisticsType()) {
            case 1:
                req.setStartYear(date.plusMonths(-12).getYear());
                req.setEndYear(date.getYear());
                List<Integer[]> yearMonths = new ArrayList<>();
                for (int i = 0; i < 12; i++) {
                    int year = date.plusMonths(-i).getYear();
                    int month = date.plusMonths(-i).getMonthValue();
                    Integer[] ints = {year, month};
                    yearMonths.add(ints);
                }
                //按月查询趋势表中每月趋势平稳度平均值
                List<StatisticsTrendstabilityResp> trendstability = statisticsTrendstabilityMapper.getList(req);
                for (Integer[] i : yearMonths) {
                    List<StatisticsTrendstabilityResp> data = trendstability.stream().filter(t -> t.getYear().equals(i[0]) && t.getMonth().equals(i[1])).collect(Collectors.toList());
                    if (!data.isEmpty()) {
                        Double value = data.stream().mapToDouble(StatisticsTrendstabilityResp::getStatusValue).average().orElse(0.0);
                        healthValue.add(0, value);
                        dateValue.add(0, i[0] + "-" + i[1]);
                    } else {
                        Double value = 0.0;
                        healthValue.add(0, value);
                        dateValue.add(0, i[0] + "-" + i[1]);
                    }
                }
                resp.setDateValue(dateValue);
                resp.setTendencyValue(healthValue);
                break;
            case 2:
                //按天查询趋势表中每天趋势平稳度平均值
                req.setStartYear(date.plusDays(-30).getYear());
                req.setEndYear(date.getYear());
                List<Integer[]> monthDays = new ArrayList<>();
                for (int i = 0; i < 30; i++) {
                    int year = date.plusDays(-i).getYear();
                    int month = date.plusDays(-i).getMonthValue();
                    int day = date.plusDays(-i).getDayOfMonth();
                    Integer[] ints = {year, month, day};
                    monthDays.add(ints);
                }
                trendstability = statisticsTrendstabilityMapper.getList(req);
                for (Integer[] i : monthDays) {
                    List<StatisticsTrendstabilityResp> data = trendstability.stream().filter(t -> t.getYear().equals(i[0]) && t.getMonth().equals(i[1]) && t.getDay().equals(i[2])).collect(Collectors.toList());
                    if (!data.isEmpty()) {
                        Double value = data.stream().mapToDouble(StatisticsTrendstabilityResp::getStatusValue).average().orElse(0.0);
                        healthValue.add(0, value);
                        dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                    } else {
                        Double value = 0.0;
                        healthValue.add(0, value);
                        dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                    }
                }
                resp.setDateValue(dateValue);
                resp.setTendencyValue(healthValue);
                break;
            case 3:
                //按季查询趋势表中每15天为步长的趋势平稳度平均值
                req.setStartYear(date.plusMonths(-3).getYear());
                req.setEndYear(date.getYear());
                monthDays = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    int year = date.plusMonths(-i).getYear();
                    int month = date.plusMonths(-i).getMonthValue();
                    Integer[] ints = {year, month, 1};
                    Integer[] ints2 = {year, month, 15};
                    monthDays.add(ints2);
                    monthDays.add(ints);
                }
                trendstability = statisticsTrendstabilityMapper.getList(req);
                for (Integer[] i : monthDays) {
                    double value;
                    int month;
                    int year;
                    if (i[2] == 1) {
                        if (i[1] - 1 == 0) {
                            month = 12;
                            year = i[0] - 1;
                        } else {
                            month = i[1] - 1;
                            year = i[0];
                        }
                        List<StatisticsTrendstabilityResp> data = trendstability.stream().filter(t -> t.getYear() == year && t.getMonth() == month && t.getDay() > 15 && t.getDay() <= 31).collect(Collectors.toList());
                        if (!data.isEmpty()) {
                            value = data.stream().mapToDouble(StatisticsTrendstabilityResp::getStatusValue).average().orElse(0.0);
                            healthValue.add(0, value);
                            dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                        } else {
                            value = 0.0;
                            healthValue.add(0, value);
                            dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                        }

                    } else {
                        List<StatisticsTrendstabilityResp> data = trendstability.stream().filter(t -> Objects.equals(t.getYear(), i[0]) && Objects.equals(t.getMonth(), i[1]) && t.getDay() >= 1 && t.getDay() <= 15).collect(Collectors.toList());
                        if (!data.isEmpty()) {
                            value = data.stream().mapToDouble(StatisticsTrendstabilityResp::getStatusValue).average().orElse(0.0);
                            healthValue.add(0, value);
                            dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                        } else {
                            value = 0.0;
                            healthValue.add(0, value);
                            dateValue.add(0, i[0] + "-" + i[1] + "-" + i[2]);
                        }
                    }
                }
                resp.setDateValue(dateValue);
                resp.setTendencyValue(healthValue);
                break;
            default:
                break;
        }
        return CommonResult.success(resp);
    }


}
